import { toast } from "../../components/Toast/Toast";
import { keywordApi } from "../backend/server";
// import { getKeywordList } from "../common/common";
import { KeywordListType } from "../domain/keyword";

const getKey = (entry: string[]) =>
  entry.find((val) => /^[a-zA-Z0-9]*$/.test(val));

export const install = async (
  val: string,
  keywordList: KeywordListType,
  clearInputCallBack: () => void
): Promise<void> => {
  const entry: string[] = val.replace("check ", "").split(" ");
  const key = getKey(entry);
  clearInputCallBack();
  // if (keywordList.findIndex((val) => val.keyName === key) > -1) {
  //   let shortcut = keywordList.find((ele) => ele.keyName === key)?.url;
  //   toast(shortcut + "");
  //   return;
  // }
  // toast("key does not exist!");
  if (!key) return toast("cannot detect key, please check your grammar!");
  const { data } = await keywordApi.findByKey(key);
  if (data === null) return toast("key does not exist!");

  toast(`${data.keyName}: ${data.url}`);
};
